//线段树-多个区间操作(乘法和加法优先级问题)
#include <iostream>
using namespace std;
#define lc p << 1
#define rc p << 1 | 1
typedef long long ll;
const int N = 1e5 + 10;
ll n, q, m;
ll a[N];

struct node
{
    ll l, r, sum, mul, add;
}tr[N << 2];

void pushup(ll p)
{
    tr[p].sum = (tr[lc].sum + tr[rc].sum) % m;
}

void build(ll p, ll l, ll r)
{
    tr[p] = {l, r, a[l], 1, 0};
    if(l == r) return;
    ll mid = (l + r) >> 1;
    build(lc, l, mid);
    build(rc, mid+1, r);
    pushup(p);
}

void lazy(ll p, ll mul, ll add)
{
    tr[p].sum = (tr[p].sum * mul + add * (tr[p].r - tr[p].l + 1)) % m;
    tr[p].mul = tr[p].mul * mul % m;
    tr[p].add = (tr[p].add * mul + add) % m;
}

void pushdown(ll p)
{
    lazy(lc, tr[p].mul, tr[p].add);
    lazy(rc, tr[p].mul, tr[p].add);
    tr[p].mul = 1, tr[p].add = 0;
}


void modify(ll p, ll x, ll y, ll mul, ll add)
{
    ll l = tr[p].l, r = tr[p].r;
    if(x <= l && r <= y)
    {
        lazy(p, mul, add);
        return;
    }
    pushdown(p);
    ll mid = (l + r) >> 1;
    if(x <= mid) modify(lc, x, y, mul, add);
    if(y > mid) modify(rc, x, y, mul, add);
    pushup(p);
}

ll query(ll p, ll x, ll y)
{
    ll l = tr[p].l, r = tr[p].r;
    if(x <= l && r <= y) return tr[p].sum;
    pushdown(p);
    ll mid = (l + r) >> 1, sum = 0;
    if(x <= mid) sum += query(lc, x, y);
    if(y > mid) sum += query(rc, x, y);
    return sum % m;
}

int main()
{
    cin >> n >> q >> m;
    for(int i = 1; i <= n; i++) cin >> a[i];
    build(1, 1, n);
    while(q--)
    {
        ll op, x, y, k; cin >> op >> x >> y;
        if(op == 1)
        {
            cin >> k;
            modify(1, x, y, k, 0);
        }
        else if(op == 2)
        {
            cin >> k;
            modify(1, x, y, 1, k);
        }
        else
        {
            cout << query(1, x, y) << endl;
        }
    }
    return 0;
}